package com.szh.zx.javacorecorret.chap1;

import java.util.concurrent.Semaphore;

public class Service {
	// todo  注意测试传2和传1是不一样的
	private Semaphore semaphore =new Semaphore(2);
	public void testMethod(){
		try {
			semaphore.acquire();
			System.out.println(Thread.currentThread().getName()+"：begin time:"+System.currentTimeMillis());
			Thread.sleep(2000);
			System.out.println(Thread.currentThread().getName()+"：end time:"+System.currentTimeMillis());

			semaphore.release();
		}catch (InterruptedException e){
			e.printStackTrace();
		}

	}
// 不要用 @Test 测试多线程，测试不出来效果
	public static void main(String[] args) {
		System.out.println("SSSSSSSS");
		Service service = new Service();
		ThreadA threadA = new ThreadA(service);
		ThreadB threadB = new ThreadB(service);
		ThreadC threadC = new ThreadC(service);

		threadA.setName("A");
		threadB.setName("B");
		threadC.setName("C");

		threadA.start();
		threadB.start();
		threadC.start();
		System.out.println("AAAAAAAAAAAAAAAAA");
	}


}
class ThreadA extends Thread{
	private Service service;

	public ThreadA(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.testMethod();
	}
}
class ThreadB extends Thread{
	private Service service;

	public ThreadB(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.testMethod();
	}
}
class ThreadC extends Thread{
	private Service service;

	public ThreadC(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.testMethod();
	}
}
